﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerPawn : MonoBehaviour
{
    RoleBehaviour _roleBehaviour;
    public List<SupplyItem> itemList;

    private SupplyItem templet;

    public TileObject currentTile;

    [SerializeField]
    private int m_Energy;

    public int energy
    {
        get
        {
            return m_Energy;
        }
        set
        {
            m_Energy = value;
            if (m_Energy == 0)
            {
                GameManager.Instance.OnFail();
            }
        }
    }

    private void Awake()
    {
        _roleBehaviour = GetComponent<RoleBehaviour>();

        templet = transform.GetChild(0).GetComponent<SupplyItem>();
    }
    // Start is called before the first frame update
    void Start()
    {
        GameManager.Instance.playerPawn = this;
        itemList = new List<SupplyItem>();
        MoveToTile(currentTile);

        energy = GameManager.GetData().MaxEnergy;
    }

    // Update is called once per frame
    void Update()
    {
        if (GameManager.Instance.isGameOver)
        {
            return;
        }

        // Process Inputs
        if (Input.GetButtonDown("MoveLeft"))
        {
            Debug.Log("MoveLeft");
            _roleBehaviour.SetAnimation(MoveStatus.LEFT);
            TileObject nextTile = currentTile;
            if (currentTile.TryLeft(out nextTile))
            {
                MoveToTile(nextTile);
            }
        }
        if (Input.GetButtonDown("MoveRight"))
        {
            Debug.Log("MoveRight");
            _roleBehaviour.SetAnimation(MoveStatus.RIGHT);
            TileObject nextTile = currentTile;
            if (currentTile.TryRight(out nextTile))
            {
                MoveToTile(nextTile);
            }
        }
        if (Input.GetButtonDown("MoveUp"))
        {
            Debug.Log("MoveUp");
            _roleBehaviour.SetAnimation(MoveStatus.UP);
            TileObject nextTile = currentTile;
            if (currentTile.TryUp(out nextTile))
            {
                MoveToTile(nextTile);
            }
        }
        if (Input.GetButtonDown("MoveDown"))
        {
            Debug.Log("MoveDown");
            _roleBehaviour.SetAnimation(MoveStatus.DOWN);
            TileObject nextTile = currentTile;
            if (currentTile.TryDown(out nextTile))
            {
                MoveToTile(nextTile);
            }
        }
        if (Input.GetButtonDown("Interact"))
        {
            Debug.Log("Interact");
            _roleBehaviour.SetAnimation(MoveStatus.IDLE);
            TryInteractWithTile(currentTile);
        }
        if (Input.GetButtonDown("Success"))
        {
            Debug.Log("Success");
            if (TryBuild())
            {
                GameManager.Instance.OnSuccess();
                _roleBehaviour.SetSuccessAnimation();
            }
            else
            {
                _roleBehaviour.SetAnimation(MoveStatus.IDLE);
            }

        }
        if (Input.GetButtonDown("Discard"))
        {
            Debug.Log("Discard");
            if (itemList.Count > 0)
            {
                var effectObj = FXManager.PlayFX("SupplyDiscard", transform);
                effectObj.transform.localPosition = itemList[itemList.Count - 1].transform.localPosition;
                DestorySupplyObject(itemList.Count - 1);
            }
            _roleBehaviour.SetAnimation(MoveStatus.IDLE);
        }

    }

    public void MoveToTile(TileObject newTile)
    {
        currentTile = newTile;
        transform.position = currentTile.transform.position;

        --energy;

        GameManager.Instance.OnMove();
    }

    public bool TryInteractWithTile(TileObject targetTile)
    {
        Debug.Log(targetTile.content);
        if (targetTile.content)
        {
            targetTile.content.OnInteract(this);
            return true;
        }
        else
        {
            return false;
        }
    }

    public bool TryAddSupply(ESupplyType supplyType)
    {
        if (itemList.Count < GameManager.GetData().MaxCarryItems)
        {
            AddSupplyObject(supplyType, itemList.Count);
            FXManager.PlayFX("SupplyPickup",transform);
            return true;
        }
        else
        {
            return false;
        }
    }

    public bool TryFulfillSupply(ESupplyType type)
    {
        for(int i = 0; i < itemList.Count; i++)
        {
            if (itemList[i].type == type)
            {
                DestorySupplyObject(i);
                FXManager.PlayFX("SupplyDeliver", transform);
                return true;
            }
        }
        return false;
    }

    private void AddSupplyObject(ESupplyType supplyType, int index)
    {
        var supplyObj = GameObject.Instantiate(templet, transform);
        var supplyItem = supplyObj.GetComponent<SupplyItem>();
        supplyItem.SetSupplyData(supplyType, index);
        itemList.Add(supplyItem);
    }

    private void DestorySupplyObject(int index)
    {
        GameObject.Destroy(itemList[index].gameObject);
        itemList.RemoveAt(index);
        for (int i = 0; i < itemList.Count; ++i)
        {
            itemList[i].SetPositoion(i);
        }
    }

    public bool TryBuild()
    {
        if (GameManager.Instance.IsScoreEnough() && currentTile.TryBuild())
        {
            //gameObject.SetActive(false);
            FXManager.PlayFX("Build", currentTile.transform);
            for (int i = 0; i < itemList.Count; ++i)
            {
                GameObject.Destroy(itemList[i].gameObject);
                
            }
            itemList.Clear();
            return true;
        }

        return false;
    }
}
